/// Copyright 2007-8 Faraday Media
/// 
/// Licensed under the Apache License, Version 2.0 (the "License"); 
/// you may not use this file except in compliance with the License. 
/// You may obtain a copy of the License at 
/// 
///   http://www.apache.org/licenses/LICENSE-2.0 
/// 
/// Unless required by applicable law or agreed to in writing, software 
/// distributed under the License is distributed on an "AS IS" BASIS, 
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
/// See the License for the specific language governing permissions and 
/// limitations under the License.
/// Copyright 2007-8 Faraday Media
/// 
/// Licensed under the Apache License, Version 2.0 (the "License"); 
/// you may not use this file except in compliance with the License. 
/// You may obtain a copy of the License at 
/// 
///   http://www.apache.org/licenses/LICENSE-2.0 
/// 
/// Unless required by applicable law or agreed to in writing, software 
/// distributed under the License is distributed on an "AS IS" BASIS, 
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
/// See the License for the specific language governing permissions and 
/// limitations under the License.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace MetaSharp.XML {
  /// <summary>
  /// Interface for a dictionary that only support read operations.
  /// </summary>
  /// <typeparam name="TKey">key type</typeparam>
  /// <typeparam name="TValue">value type</typeparam>
  public interface IReadOnlyDictionary<TKey, TValue> : IEnumerable<KeyValuePair<TKey, TValue>>, IEnumerable {
    /// <summary>
    /// Checks if the dictionary contains the given key.
    /// </summary>
    /// <param name="key">the key to check</param>
    /// <returns>true - contains the key</returns>
    bool ContainsKey(TKey key);

    /// <summary>
    /// Attempts to get the value associated with the given key.
    /// </summary>
    /// <param name="key">the key</param>
    /// <param name="value">the found value, or null</param>
    /// <returns>true - the value was fetched</returns>
    bool TryGetValue(TKey key, out TValue value);

    /// <summary>
    /// Retrieves the item from the dictionary with the given key.
    /// </summary>
    /// <param name="key">the key</param>
    /// <returns>the associated item</returns>
    /// <exception cref="KeyNotFoundException">if the key is invalid</exception>
    TValue this[TKey key] { get; }

    /// <summary>
    /// The keys of the dictionary.
    /// </summary>
    ICollection<TKey> Keys { get; }

    /// <summary>
    /// The values of the dictionary.
    /// </summary>
    ICollection<TValue> Values { get; }

    /// <summary>
    /// Whether the dictionary contains the given pair.
    /// </summary>
    /// <param name="item">the item to check for</param>
    /// <returns>true - contains the pair</returns>
    bool Contains(KeyValuePair<TKey, TValue> item);

    /// <summary>
    /// Copies the elements of the dictionary to an array.
    /// </summary>
    /// <param name="array">the array</param>
    /// <param name="arrayIndex">starting array index</param>
    void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex);

    /// <summary>
    /// Removes the given element from the dictionary.
    /// </summary>
    /// <param name="item">item to remove</param>
    /// <returns>true - the item was removed</returns>
    bool Remove(KeyValuePair<TKey, TValue> item);

    /// <summary>
    /// The number of items in the dictionary.
    /// </summary>
    int Count { get; }

    /// <summary>
    /// Always returns true to indiciate the dictionary is read only.
    /// </summary>
    bool IsReadOnly { get; }

    /// <summary>
    /// Retrieve a copy of this dictionary as a IDictionary. The provided object will
    /// provide exceptions on non-supported calls.
    /// </summary>
    /// <returns>an IDictionary instance</returns>
    IDictionary<TKey, TValue> AsIDictionary();
  }

  /// <summary>
  /// Wrapper for making a dictionary read only.
  /// </summary>
  /// <typeparam name="TKey">key type</typeparam>
  /// <typeparam name="TValue">value type</typeparam>
  public class ReadOnlyDictionary<TKey, TValue> : IReadOnlyDictionary<TKey, TValue>, IDictionary<TKey, TValue>, IEnumerable<KeyValuePair<TKey, TValue>>, IEnumerable {
    private readonly IDictionary<TKey, TValue> mUnderlying;

    /// <summary>
    /// Creates a new ReadOnly dictionary, wrapping the given dictionary.
    /// </summary>
    /// <param name="pUnderlying">dictionary to wrap</param>
    public ReadOnlyDictionary(IDictionary<TKey, TValue> pUnderlying) {
      mUnderlying = pUnderlying;
    }

    /// <summary>
    /// Retrieve a copy of this dictionary as a IDictionary. The provided object will
    /// provide exceptions on non-supported calls.
    /// </summary>
    /// <returns>an IDictionary instance</returns>
    public IDictionary<TKey, TValue> AsIDictionary() {
      return this;
    }

    #region IDictionary<TKey,TValue> Members

    /// <summary>
    /// Checks if the dictionary contains the given key.
    /// </summary>
    /// <param name="key">the key to check</param>
    /// <returns>true - contains the key</returns>
    public bool ContainsKey(TKey key) {
      return mUnderlying.ContainsKey(key);
    }

    ///<summary>
    ///Adds an element with the provided key and value to the <see cref="T:System.Collections.Generic.IDictionary`2"></see>.
    ///</summary>
    ///<param name="value">The object to use as the value of the element to add.</param>
    ///<param name="key">The object to use as the key of the element to add.</param>
    ///<exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.IDictionary`2"></see> is read-only.</exception>
    ///<exception cref="T:System.ArgumentException">An element with the same key already exists in the <see cref="T:System.Collections.Generic.IDictionary`2"></see>.</exception>
    ///<exception cref="T:System.ArgumentNullException">key is null.</exception>
    public void Add(TKey key, TValue value) {
      throw new NotImplementedException();
    }

    /// <summary>
    /// Removes the element with the specified key from the <see cref="T:System.Collections.Generic.IDictionary`2"></see>.
    /// </summary>
    /// <returns>
    /// true if the element is successfully removed; otherwise, false.  This method also returns false if key was not found in the original <see cref="T:System.Collections.Generic.IDictionary`2"></see>.
    /// </returns>
    /// <param name="key">The key of the element to remove.</param>
    /// <exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.IDictionary`2"></see> is read-only.</exception>
    /// <exception cref="T:System.ArgumentNullException">key is null.</exception>
    public bool Remove(TKey key) {
      throw new NotImplementedException();
    }

    /// <summary>
    /// Attempts to get the value associated with the given key.
    /// </summary>
    /// <param name="key">the key</param>
    /// <param name="value">the found value, or null</param>
    /// <returns>true - the value was fetched</returns>
    public bool TryGetValue(TKey key, out TValue value) {
      return mUnderlying.TryGetValue(key, out value);
    }

    /// <summary>
    /// Retrieves the item from the dictionary with the given key.
    /// </summary>
    /// <param name="key">the key</param>
    /// <returns>the associated item</returns>
    /// <exception cref="KeyNotFoundException">if the key is invalid</exception>
    public TValue this[TKey key] {
      get { return mUnderlying[key]; }
      set { throw new NotImplementedException(); }
    }

    /// <summary>
    /// The keys of the dictionary.
    /// </summary>
    public ICollection<TKey> Keys {
      get { return new ReadOnlyCollection<TKey>(new List<TKey>(mUnderlying.Keys)); }
    }

    /// <summary>
    /// The values of the dictionary.
    /// </summary>
    public ICollection<TValue> Values {
      get { return new ReadOnlyCollection<TValue>(new List<TValue>(mUnderlying.Values)); }
    }

    #endregion

    #region ICollection<KeyValuePair<TKey,TValue>> Members

    ///<summary>
    ///Adds an item to the <see cref="T:System.Collections.Generic.ICollection`1"></see>.
    ///</summary>
    ///<param name="item">The object to add to the <see cref="T:System.Collections.Generic.ICollection`1"></see>.</param>
    ///<exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.ICollection`1"></see> is read-only.</exception>
    public void Add(KeyValuePair<TKey, TValue> item) {
      throw new NotImplementedException();
    }

    ///<summary>
    ///Removes all items from the <see cref="T:System.Collections.Generic.ICollection`1"></see>.
    ///</summary>
    ///<exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.ICollection`1"></see> is read-only. </exception>
    public void Clear() {
      throw new NotImplementedException();
    }

    /// <summary>
    /// Whether the dictionary contains the given pair.
    /// </summary>
    /// <param name="item">the item to check for</param>
    /// <returns>true - contains the pair</returns>
    public bool Contains(KeyValuePair<TKey, TValue> item) {
      return mUnderlying.Contains(item);
    }

    /// <summary>
    /// Copies the elements of the dictionary to an array.
    /// </summary>
    /// <param name="array">the array</param>
    /// <param name="arrayIndex">starting array index</param>
    public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex) {
      mUnderlying.CopyTo(array, arrayIndex);
    }

    /// <summary>
    /// Removes the given element from the dictionary.
    /// </summary>
    /// <param name="item">item to remove</param>
    /// <returns>true - the item was removed</returns>
    public bool Remove(KeyValuePair<TKey, TValue> item) {
      throw new NotImplementedException();
    }

    /// <summary>
    /// The number of items in the dictionary.
    /// </summary>
    public int Count {
      get { return mUnderlying.Count; }
    }

    /// <summary>
    /// Always returns true to indiciate the dictionary is read only.
    /// </summary>
    public bool IsReadOnly {
      get { return true; }
    }

    #endregion

    #region IEnumerable<KeyValuePair<TKey,TValue>> Members

    IEnumerator<KeyValuePair<TKey, TValue>> IEnumerable<KeyValuePair<TKey, TValue>>.GetEnumerator() {
      return mUnderlying.GetEnumerator();
    }

    #endregion

    #region IEnumerable Members

    ///<summary>
    ///Returns an enumerator that iterates through a collection.
    ///</summary>
    ///<returns>
    ///An <see cref="T:System.Collections.IEnumerator"></see> object that can be used to iterate through the collection.
    ///</returns>
    ///<filterpriority>2</filterpriority>
    public IEnumerator GetEnumerator() {
      return mUnderlying.GetEnumerator();
    }

    #endregion
  }
}
